<?php
/**
 * 电影店铺里面的二级目录
 */
namespace AdminV3\Controller;
use Think\Page;


class MovieLevel2Controller extends CommonController{

    public function index()
    {
        $where['movie_id'] = I('get.movie_id');
        $list=M('MovieLevel2V3')->where($where)->select();
        $this->assign('list', $list);
        $this->assign('movie_id', $where['movie_id']);

        $parameters = unserialize(base64_decode(session('movie_v3_url_parameters')));
        $this->assign('parameters',$parameters);

        $this->display();
    }


    public function add()
    {
        if (!empty($_POST)) {

            $data['name'] = trim(I('post.name'));
            $data['movie_id'] = trim(I('post.movie_id'));

            $data['sort'] = trim(I('post.sort'));
            $data['status'] = trim(I('post.status'));

            if(in_array('',$data)){
                $this->error('数据不完整');
            }

            //$data['url'] = trim(I('post.url'));

            //上传图片
//            if(!empty ($_FILES)){
//                foreach ($_FILES as $name => $value) {
//                    if (is_file($value['tmp_name'])) {
//                        $this->_uploadImage($data, $name, $value);
//                    }
//                }
//            }

            $data['create_time'] = time();
            $data['last_update_time'] = time();

            if (M('MovieLevel2V3')->add($data)) {
                $this->redirect('MovieLevel2/index',array('movie_id'=>$data['movie_id']));
            } else {
                $this->error('添加失败');
            }
        } else {

            $this->assign('movie_id',trim(I('get.movie_id')));
            $this->display();
        }
    }


    public function edit()
    {
        if (!empty($_GET)) {
            $id = I('get.movie_level2_id');
           // var_dump($_GET);die();
            if (!empty($id)) {
                $where['id']=$id;
                $data=D('MovieLevel2V3')->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('非法操作! 没有ID值.');
            }
        }


        if (!empty($_POST)) {
            $data['id'] = trim(I('post.id'));
            if (empty($data['id'])) {
                $this->error('非法操作! 没有ID值.');
            }

            $data['movie_id'] = trim(I('post.movie_id'));
            if (empty($data['id'])) {
                $this->error('非法操作! 没有movie_ID值.');
            }


            $data['name'] = trim(I('post.name'));
            $data['sort'] = trim(I('post.sort'));
            $data['status'] = trim(I('post.status'));
               

                if (in_array('', $data)) {
                    $this->error('信息没有填写完整!');
                }

            //$data['url'] = trim(I('post.url'));

//                if(!empty ($_FILES)){
//                    foreach ($_FILES as $name => $value) {
//                        if (is_file($value['tmp_name'])) {
//                            $this->_uploadImage($data, $name, $value);
//                        }
//                    }
//                }


                $data['last_update_time'] =time();

              
                if (M('MovieLevel2')->save($data) > 0) {
                    $this->redirect('MovieLevel2/index',array('movie_id'=>$data['movie_id']));
                } else {
                    $this->error('编辑信息失败!');
                }

        }
    }


    //修改商品的状态
    public function alter()
    {
        $idList = I('post.idlist');
        $type = I('post.type');
        $result = array();
        if (!empty($type)) {
            if (!empty($idList)) {
                $idList = rtrim($idList, ',');
                //var_dump($idList);
                $idList = explode(',', $idList);
                //var_dump($idList);
                $data['ok'] = 0;
                $data['error'] = 0;

                foreach ($idList as $k => $val) {
                    //通过审核
                    switch ($type) {
                        case 'on':
                            $newType = 1;
                            break;
                        case 'off':
                            $newType = 2;
                            break;
                        case 'del':
                            $newType = 3;
                            break;
                    }

                    $re = M('MovieLevel2V3')->where('id=' . $val)->setField('status', $newType);
                    if ($re > 0) {
                        $data['ok']++;
                    } else {
                        $data['error']++;
                    }
                }
                $result['mes'] = "操作成功:{$data['ok']},失败{$data['error']}";
                $this->ajaxReturn($result);

            } else {
                $result['mes'] = '没有选择的值';
                $this->ajaxReturn($result);
            }
        } else {
            $result['mes'] = '非法操作!';
            $this->ajaxReturn($result);
        }

    }

}